package cn.csust.net2.manager.server.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import cn.csust.net2.manager.client.ux.ExpandTree;
import cn.csust.net2.manager.server.dao.AcademyDAO;
import cn.csust.net2.manager.server.security.AuthCurrentUser;
import cn.csust.net2.manager.server.util.Util;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.Major;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.po.Teacher;
import cn.csust.net2.manager.shared.po.User;
import cn.csust.net2.manager.shared.service.TreeService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.vo.TreeNode;

public class TreeServiceImpl extends BaseServiceImpl implements TreeService{
	@Resource(name = Constant.DAO_NAME_ACADEMY)
	private AcademyDAO academyDAO;
	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<TreeNode> getChildren(TreeNode loadConfig,Map<String,Object> configs)throws BeyondAuthorityException {
		String[] items = null ;
		boolean teacher_add = false;
//		List<PO> list = new ArrayList<PO> ();
//		list.add(new Student());
		if(configs.get(ExpandTree.TEACHER_ADD)!=null)
			teacher_add = (Boolean) configs.get(ExpandTree.TEACHER_ADD);
		if(configs.get(ExpandTree.GRADE_ITEMS)!=null)
			items = (String[]) configs.get(ExpandTree.GRADE_ITEMS);
		/**
		 * 对学院的加载
		 */
		User user = AuthCurrentUser.get();
		if (loadConfig==null){
			ArrayList<?> list  ;
			String role = this.getRole();
			if(role.equals("student")){
				list = (ArrayList<?>) academyDAO.findListByHQL_T(" from Academy c where c.academyID = "+((Student)user).getBanji().getMajor().getAcademy().getAcademyID());
			}else if(role.equals("teacher")){
				list =(ArrayList<?>) academyDAO.findListByHQL_T(" from Academy where c.academyID =  "+((Teacher)user).getAcademy().getAcademyID());
			}else{
				list  =  (ArrayList<?>) academyDAO.findAll();
			}
			return (List<TreeNode>) list;
		}
		else{
			
			TreeNode treeNode = loadConfig;
			Class<?> [] children = treeNode.getChildren();
			Class<?> self = treeNode.getSelf();
			List<TreeNode> list = new ArrayList<TreeNode>();
			StringBuffer gradeStr = new StringBuffer();
			if(self.equals(Major.class)&&items!=null){
				gradeStr.append(" AND ( 1=0 ");
				for(int i = 0;i<4;i++){
					if(items[i]!=null)
						gradeStr.append(" OR c.grade ="+items[i]);	
				}
				gradeStr.append(" ) ");
			}
			if(teacher_add){
				for(int i=0;i<children.length;i++){
				
					list.addAll((List<TreeNode>)academyDAO.findListByHQL_O("from "+children[i].getSimpleName()+" as c where c."+Util.firstToLow(self.getSimpleName())+"." + treeNode.getNodeIdName() +" = "+treeNode.getNodeId()+gradeStr));
			
				}
			}
			else{
			
				list.addAll((List<TreeNode>)academyDAO.findListByHQL_O("from "+children[0].getSimpleName()+" as c where c."+Util.firstToLow(self.getSimpleName())+"." + treeNode.getNodeIdName() +" = "+treeNode.getNodeId()+gradeStr));
				
			}
			return 	list;
		}
	}
}
